<template>
  <div>
    <div v-show="!isEdit&&!isDetail" class="page-index">
      <div class="page-left">
        <ClassifyTreeSilder
          ref="treeSilder"
          @nodeClick="nodeClick"
          :isEdit="true"
          @del="del"
          @add="add"
          @edit="edit"
        />
      </div>
      <div class="page-right">
        <div class="table-search">
          <el-form inline>
            <el-form-item label="标题">
              <el-input v-model="queryParams.title" size="mini" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="来源/发布人">
              <el-input v-model="queryParams.source" size="mini" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="发布状态">
              <el-select v-model="queryParams.status" clearable size="mini" placeholder="请选择">
                <el-option
                  v-for="(item,index) in releaseList"
                  :key="index"
                  :label="item"
                  :value="index"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="mini" type="primary" @click="search">搜索</el-button>
              <el-button size="mini" @click="reset">重置</el-button>
            </el-form-item>
          </el-form>
          <el-form inline>
            <el-form-item>
              <el-button size="mini" type="primary" @click="openAdd">新增</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="table-box">
          <el-table
            :data="tableData"
            height="100%"
            ref="table"
          >
            <el-table-column prop="id" align="center" label="ID" width="80" />
            <el-table-column prop="title" label="标题" align="center" min-width="240" />
            <el-table-column prop="type" label="所在分类" align="center" min-width="160" />
            <el-table-column prop="sort" label="分类排序" align="center" width="120" />
            <el-table-column prop="status" label="发布状态" align="center" width="120">
              <template slot-scope="scope">
                <el-switch
                  v-if="scope.row.status"
                  v-model="scope.row.status"
                  :inactive-value="1"
                  :active-value="2"
                  @change="(val)=>statusChange(val,scope.row)"
                />
                <span v-else>草稿</span>
              </template>
            </el-table-column>
            <el-table-column prop="affiliationId" label="可见范围" align="center" min-width="160" />
            <el-table-column prop="source" label="来源/发布人" align="center" min-width="120" />
            <el-table-column prop="releaseTime" label="发布时间" align="center" width="160" />
            <el-table-column fixed="right" label="操作" align="center" width="160">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="mini"
                  @click="openDetail(scope.row)"
                >查看</el-button>
                <el-button
                  type="text"
                  size="mini"
                  @click="openEdit(scope.row)"
                >编辑</el-button>
                <el-button
                  type="text"
                  size="mini"
                  style="color:#F56C6C;"
                  @click="openDelete(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getData"
        />
      </div>
      <addOrg ref="addOrg" @updateTree="updateTree" />
      <editOrg ref="editOrg" @updateTree="updateTree" />
    </div>
    <addOrEdit v-show="isEdit" ref="addOrEdit" @getData="getData" @goback="editToback" />
    <detailPage v-show="isDetail" ref="detailPage" @goback="detailToback" @detailToEdit="detailToEdit" />
  </div>
</template>
<script>
import addOrEdit from './components/addOrEdit'
import detailPage from './components/detail'
import addOrg from './components/addOrg.vue'
import editOrg from './components/editOrg.vue'
import ClassifyTreeSilder from './components/classifyTreeSilder'
import { treeDelete, queryByCondition, deleteById, edit } from '@/api/onlineClassroom/partyConstitutionRegulations'
export default {
  components: {
    addOrEdit,
    detailPage,
    ClassifyTreeSilder,
    addOrg,
    editOrg
},
  data () {
    return {
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        typeId: '',
        title: '',
        source: '',
        status: ''
      },
      total: 0,
      tableData: [],
      releaseList: ['草稿', '未发布', '已发布'],
      isEdit: false,
      isDetail: false
    }
  },
  mounted () {
    this.getData()
  },
  methods: {
    getData () {
      queryByCondition(this.queryParams).then((res) => {
        this.tableData = res?.data?.records || []
        this.total = res?.data?.total || 0
      })
    },
    add (id, label) {
      this.$refs.addOrg.open(id, label)
    },
    edit (id, level) {
      this.$refs.editOrg.open(id, level)
    },
    del (id, level) {
      this.$confirm('此操作将永久删除该分组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        treeDelete({ id }).then((res) => {
          this.$message.success('删除成功')
          this.updateTree(0)
        })
      })
    },
    updateTree (id) {
      this.$refs.treeSilder.updateKeyData(id)
    },
    nodeClick (node) {
      this.queryParams.typeId = node.id || null
      this.search()
    },
    search () {
      this.queryParams.pageNo = 1
      this.getData()
    },
    openAdd () {
      this.isEdit = true
      this.$refs.addOrEdit.init({})
    },
    editToback (isQuery) {
      this.isEdit = false
      if (isQuery) {
        this.getData()
      }
    },
    statusChange (val, row) {
      const obj = { ...row }
      obj.status = val
      edit(obj).then((res) => {
        this.$message.success('修改成功')
        this.getData()
      })
    },
    detailToback () {
      this.isDetail = false
    },
    openDetail (row) {
      this.isDetail = true
      this.$refs.detailPage.init(row)
    },
    openEdit (row) {
      this.isEdit = true
      this.$refs.addOrEdit.init(row)
    },
    detailToEdit (row) {
      this.isDetail = false
      this.openEdit(row)
    },
    openDelete (row) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById({ id: row.id }).then((res) => {
          this.$message.success('删除成功')
          this.getData()
        })
      })
    },
    reset () {
      this.queryParams.title = ''
      this.queryParams.source = ''
      this.queryParams.typeId = ''
      this.queryParams.status = ''
      this.search()
    }
  }
}
</script>
<style scoped lang="scss">
.page-index {
  padding-top: 24px;
  display: flex;
  .page-left {
    width: 300px;
  }
  .page-right {
    flex: 1;
    overflow: hidden;
  }
  .table-search {
    display: flex;
    justify-content: space-between;
    padding-left:16px;
  }
  .table-box {
    height: calc(100vh - 230px);
  }
}

</style>
